package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.entity.BusinessData;
import com.itheima.health.entity.HotSetmealData;
import com.itheima.health.entity.MemberReport;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.SetmealDto;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 数据报告控制器
 */
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {
    @Autowired
    private ReportService reportService;

    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 统计分析-套餐预约占比饼形图
     *
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {
        //返回的结果是自己封装的对象
        SetmealDto setmealDto = reportService.getSetmealReport();
        //返回查询成功的结果
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, setmealDto);
    }

    //运营数据统计
    //Path:report/getBusinessReportData
    //Method： GET
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        BusinessData businessData = reportService.getBusinessReportData();
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, businessData);
    }

    @GetMapping("getMemberReport")
    public Result getMemberReport() {
        MemberReport memberNumber = memberService.getMemberReport();
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, memberNumber);
    }

    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {

        BusinessData businessData = reportService.getBusinessReportData();
        Integer todayNewMember = memberDao.todayNewMember();
        Integer weekNewMember = memberDao.thisWeekNewMember();
        Integer monthNewMember = memberDao.thisMonthNewMember();
        Integer totalMember = memberDao.totalMember();
        //查询订单信息
        Integer todayOrderNumber = orderDao.todayOrderNumber();
        Integer thisWeekOrderNumber = orderDao.thisWeekOrderNumber();
        Integer thisMonthOrderNumber = orderDao.thisMonthOrderNumber();
        Integer todayVisitsNumber = orderDao.todayVisitsNumber();
        Integer thisWeekVisitsNumber = orderDao.thisWeekVisitsNumber();
        Integer thisMonthVisitsNumber = orderDao.thisMonthVisitsNumber();
        //查询套餐数据
        List<HotSetmealData> hotSetmealDatas = orderDao.hotSetmeal();
        for (HotSetmealData hotSetmealData : hotSetmealDatas) {
            hotSetmealData.setProportion(hotSetmealData.getSetmeal_count().divide(orderDao.totalOrderNumber(), 4, BigDecimal.ROUND_HALF_UP));
        }
        //日期
        String reportDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        try {
            XSSFWorkbook excel = new XSSFWorkbook(this.getClass().getClassLoader().getResourceAsStream("report_template.xlsx"));
            //读取第一张表
            XSSFSheet sheetAt = excel.getSheetAt(0);
            //获取第三行
            XSSFRow row = sheetAt.getRow(2);
            XSSFCell cell = row.getCell(5);
            cell.setCellValue(reportDate);
            row = sheetAt.getRow(4);
            cell = row.getCell(5);
            cell.setCellValue(todayNewMember);
            row = sheetAt.getRow(4);
            cell = row.getCell(7);
            cell.setCellValue(totalMember);
            row = sheetAt.getRow(5);
            cell = row.getCell(5);
            cell.setCellValue(weekNewMember);
            row = sheetAt.getRow(5);
            cell = row.getCell(7);
            cell.setCellValue(monthNewMember);
            row = sheetAt.getRow(7);
            cell = row.getCell(5);
            cell.setCellValue(todayOrderNumber);
            row = sheetAt.getRow(7);
            cell = row.getCell(7);
            cell.setCellValue(todayVisitsNumber);
            row = sheetAt.getRow(8);
            cell = row.getCell(5);
            cell.setCellValue(thisWeekOrderNumber);
            row = sheetAt.getRow(8);
            cell = row.getCell(7);
            cell.setCellValue(thisWeekVisitsNumber);
            row = sheetAt.getRow(9);
            cell = row.getCell(5);
            cell.setCellValue(thisMonthOrderNumber);
            row = sheetAt.getRow(9);
            cell = row.getCell(7);
            cell.setCellValue(thisMonthVisitsNumber);
            int rewRow = 12;
            for (HotSetmealData setmealData : hotSetmealDatas) {
                String name = setmealData.getName();
                BigDecimal setmealcount = setmealData.getSetmeal_count();
                BigDecimal proportion = setmealData.getProportion();
                row = sheetAt.getRow(rewRow++);
                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmealcount.longValue());
                row.getCell(6).setCellValue(proportion.doubleValue());
            }
                //输出到数据
                ServletOutputStream outputStream = response.getOutputStream();
                response.setContentType("application/vnd.ms-excel");//代表是=Excel文件类型
                response.setHeader("content-Disposition", "attachment:filename=report.xlsx");//指定以附件形式进行下载
                excel.write(outputStream);
                outputStream.flush();
                outputStream.close();
                excel.close();
                return null;
        } catch (Exception e) {
        }
        return new Result(false, MessageConst.GET_BUSINESS_REPORT_SUCCESS);

    }}